package com.dm0858.bianmin.ui.activity;

import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.dm0858.bianmin.R;
import com.dm0858.bianmin.ui.base.BaseActivity;
import com.dm0858.bianmin.ui.base.BasePresenter;
import com.dm0858.bianmin.utils.UIUtils;

import butterknife.Bind;
import butterknife.OnClick;
import flyn.Eyes;

/**
 * Created by yelvqing on 2018/2/10.
 */

public class LifeActivity extends BaseActivity {

    @Bind(R.id.pb_loading)
    ProgressBar mPbLoading;

    @Bind(R.id.wv_content)
    WebView mWvContent;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_life;
    }

    @Override
    public void initView() {
        Eyes.setStatusBarColor(this, UIUtils.getColor(R.color.light_green));
    }

    @Override
    public void initData() {
        String url = "https://m.hao123.com/";
        mWvContent.loadUrl(url);
    }

    @Override
    public void initListener() {
        WebSettings webSettings = mWvContent.getSettings();
        webSettings.setJavaScriptEnabled(true);


//设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

//缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

//其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

        mWvContent.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url,
                                      Bitmap favicon) {
                mPbLoading.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                mPbLoading.setVisibility(View.GONE);
                view.loadUrl("javascript:function setTop(){document.querySelector('.headPage').style.display=\"none\";}setTop();");
                view.loadUrl("javascript:function setTop(){document.querySelector('.cn_tips .box').style.display=\"none\";}setTop();");
            }
        });

        mWvContent.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                mPbLoading.setProgress(newProgress);
                view.loadUrl("javascript:function setTop(){document.querySelector('.headPage').style.display=\"none\";}setTop();");

                view.loadUrl("javascript:function setTop(){document.querySelector('.cn_tips .box').style.display=\"none\";}setTop();");
            }


        });

        mWvContent.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && mWvContent.canGoBack()) {  //表示按返回键
                        mWvContent.goBack();   //后退
                        return true;    //已处理
                    }
                }
                return false;
            }
        });
    }

    public void onPause() {
        super.onPause();
        mWvContent.onPause();
    }
    public void onResume() {
        super.onResume();
        mWvContent.onResume();
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        mWvContent.reload();
        finish();

    }
}
